﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test_PLX.DataAccess;
using Test_PLX.Domain;
using Test_PLX.Models;

namespace Test_PLX.Controllers
{
    public class LogController : Controller
    {
        // GET: Log
        //public ActionResult Index()
        //{
        //    return View();
        //}
        public ActionResult GetLogList(int personId=0)
        {
            var viewLogList = new LogDomain();
            ViewData["view_taskList"] = viewLogList.GetLogList(0);
            return View();
        }
        public string GetDayOfLastFood(int personId,string eatTime)
        {
            var db = new testEntities();
            var personLastLog = new LogDomain().GetLogList(personId)
               // .GroupBy(s => Convert.ToDateTime(s.EatTime).ToString("yyyy-MM-dd")))
               .Where(s=>Convert.ToDateTime(s.EatTime).ToString("yyyy-MM-dd") == eatTime)
                .OrderByDescending(s =>s.EatTime).First();
            var foodId = personLastLog.FoodId;
            var dayOfLastFood = db.Food.Where(s => s.FoodId == foodId).ToList().FirstOrDefault().FoodName.ToString();
            ViewBag.dayOfLastFood = dayOfLastFood;
            return dayOfLastFood;
        }
        public bool AddLog(LogModel log)
        {
            return new LogDomain().AddLog(log);
        }
        public bool AddPerson(PersonModel person)
        {
            return new LogDomain().AddPerson(person);
        }
        public bool AddFood(FoodModel food)
        {
            return new LogDomain().AddFood(food);
        }
        public bool DeleteLog(int logId)
        {
            return new LogDomain().DeleteLog(logId);
        }
        public bool ModifyLog(LogModel log)
        {
            var db = new testEntities();
            if (db.Food.Where(s => s.FoodId == log.FoodId).Count() == 0)
            {
                throw new Exception("添加food不在Food表里边，没有它的id！");
            }
            return new LogDomain().ModifyLog(log);
        }
        public ActionResult GetLog(int logId)
        {
            var db = new testEntities();
            ViewBag.LogId = logId;
            var personId =(int) db.Log.Where(s => s.LogId == logId).ToList().FirstOrDefault().PersonId;
            ViewBag.PersonId = new LogDomain().GetLogList(personId).FirstOrDefault().PersonId;
            ViewBag.EatTime = new LogDomain().GetLogList(personId).FirstOrDefault().EatTime;
            ViewBag.FoodId = new LogDomain().GetLogList(personId).FirstOrDefault().FoodId;
            return View();
        }
    }
}